<template>
  <el-form ref="modelform" :model="modelform" label-width="80px">

    <div class="alert-container clearfix">
      <div class="alert-container clearfix">
        <el-alert  v-for="alert in alerts.errors"  :key="alert.title"
                   v-bind:title="alert.content"
                   type="error"
                   show-icon>
        </el-alert>
      </div>

    </div>

    <el-form-item label="模型定义">
      <el-input type="textarea"  v-model="modelform.modelDefinition" placeholder="请输入模型定义"  clearable autosize></el-input>
    </el-form-item>


    <el-form-item>
      <el-button type="primary" @click="generateModel" >生成模型</el-button>
    </el-form-item>
    <el-form-item label="模型名称" >
      <el-input type="textarea" v-model="modelform.className" placeholder="请输入模型名称" clearable autosize></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="generateBaseCode" >生成基础代码</el-button>
    </el-form-item>
    <el-form-item label="Controller方法名称" >
      <el-input type="input" v-model="modelform.controllerMethodName" placeholder="请输入Controller方法名称，例如：com.cisdi.info.simple.controller.codetable.CodeTableController.updateCodeTable" clearable autosize></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="generateControllerMethod" >生成方法代码</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="createSuperOperator" >创建超级用户</el-button>
    </el-form-item>
    <el-form-item>
      <el-card :body-style="{ padding: '0px' }">
        <div style="padding: 14px;">
          <span>测试功能</span>
          <div class="bottom clearfix">
            <el-button type="primary" @click="testException" >异常测试</el-button>

          </div>
        </div>
      </el-card>
    </el-form-item>
    <div class="text item clearfix">
      <el-alert v-for="remark in alerts.remarks"  :key="remark.title"
                v-bind:title="remark.title"
                type="info"
                v-bind:description="remark.description">
      </el-alert>
    </div>
  </el-form>


</template>

<script>
  import {version} from '../../../package'
  import {GeneratorService} from './api';
  import {Msg} from '../../tools/message';

    export default {
      name: "generator",
      data() {
        return {
          content: "dddd",
          modelform:{
            className:"",
            modelDefinition:"",
            controllerMethodName:"",

          },
          alerts:{remarks:[{title:"功能说明",content:"TODO: 请在这里加上功能说明"},],
            successes:[], //TODO:[{title:'消息内容'},]
            infos:[],
            warnings:[],
            errors:[],},
        }
      },
      methods:{
        addAlert(message,title,type){
          //type 可选的值为:remarks,successes,infos,warnings,errors
          type = type? type:"errors";
          type = this.alerts[type]?type:"errors";
          title = title?title:message;
          this.alerts[type].unshift({title:title,content:message});
        },
        removeAlert(title,type){
          //type 可选的值为:remarks,successes,infos,warnings,errors
          type = type? type:"errors";
          type = this.alerts[type]?type:"errors";
          for (var i = this.alerts[type].length-1; i>=0 ; i--) {
            delete this.alerts[type][i]
          }
        },

        generateModel(){
          var modelform = this.modelform
          this.$refs["modelform"].validate((valid)=>{
            if (valid){
              console.log('submit!');
              GeneratorService.generateModel(modelform.modelDefinition).then((res) => {
                if(res.data.errorMsg !== "")
                {
                  Msg.error("生成模型出错："+res.data.errorMsg )
                }
                else
                {
                  Msg.success('生成模型成功'+res.data.successMsg);
                  if ( ! this.modelform.className)
                  {
                    this.modelform.className=res.data.successMsg;
                  }
                  else
                  {
                    this.modelform.className+="\n"+this.modelform.className;
                  }

                }
              }).catch(err => {
                Msg.error("生成模型出错："+err.data.error)
              })
            }
            else
            {
              return false;
            }

            }
          )
        },
        generateBaseCode(){
          var modelform = this.modelform
          this.$refs["modelform"].validate((valid)=>{
              if (valid){
                console.log('submit!');
                GeneratorService.generateBaseCode(modelform.className).then((res) => {
                  if(res.data.errorMsg !== "")
                  {
                    Msg.error("生成基础代码出错："+res.data.errorMsg )
                  }
                  else
                  {
                    Msg.success('生成基础代码成功'+res.data.successMsg);

                  }
                }).catch(err => {
                  Msg.error("生成基础代码出错："+err)
                })
              }
              else
              {
                return false;
              }

            }
          )
        },
        generateControllerMethod(){
          var modelform = this.modelform
          this.$refs["modelform"].validate((valid)=>{
              if (valid){
                GeneratorService.generateMethod(this.modelform.controllerMethodName).then((res) => {
                  Msg.success('创建超级用户成功'+res.data);
                }).catch(err => {
                  Msg.error("创建超级用户出错："+err)
                })
              }
              else
              {
                return false;
              }

            }
          )
        },
        createSuperOperator(){
          var modelform = this.modelform
          this.$refs["modelform"].validate((valid)=>{
              if (valid){
                GeneratorService.createSuperOperator().then((res) => {
                  Msg.success('生成方法代码成功'+res.data);
                }).catch(err => {
                  Msg.error("生成方法代码出错："+err)
                })
              }
              else
              {
                return false;
              }

            }
          )
        },
        testException(){

                GeneratorService.testException().then((res) => {
                  Msg.success('生成方法代码成功'+res.data);
                }).catch((error) => {

                  //error的data属性是后台传入的数据,data的extendedData存有后台传入的数据
                  this.isSubmiting = false;
                  if (error.status===590)
                  {
                    //error.data.extendedData 是扩展数据，可以使用
                    //this.alerts.errors.unshift({title:error.data.message})
                    this.addAlert(error.data.message)
                  }
                  else
                  {
                    this.alerts.errors.unshift({title:error.data.message})
                    this.$message({
                      type: 'error',
                      message: '删除失败，,原因是：'+error.data.message
                    })
                  }

                });

        },
      }
    }
</script>

<style scoped>

</style>
